package com.jwgf.queueprogramflash;

import com.jwgf.common.Utility;

public class FlashCommand {
	public static byte[] JUMP_BOOT_FLASH = { 0x55, 0x55, 0x01, 0x01 };
	public static byte[] JUMP_BOOT_NO_FLASH = { 0x55, 0x55, 0x01, 0x02 };

	public static byte[] JUMP_USER = { 0x55, 0x55, 0x02, 0x00 };

	public static byte[] ERASE_FLASH = { 0x55, 0x55, 0x03, 0x00 };
	public static byte[] QUERY_ERASE = { 0x55, 0x55, 0x04, 0x01 };

	public static byte[] PAGE_FLASH_START = { 0x55, 0x55, 0x55, 0x55 };
	public static byte[] QUERY_PAGE_FLASH = { 0x55, 0x55, 0x04, 0x02 };
	public static byte[] PAGE_FLASH_FINISH = { (byte) 0xAA, (byte) 0xAA };

	public static int BROADCAST_ID = 0x780;

	public static byte[] createJumpBootFlash(int id) {
		byte[] idArr = Utility.ByteArrayUtils.short2ByteArray((short) id);
		return Utility.ByteArrayUtils.concatAll(idArr, JUMP_BOOT_FLASH);
	}

	public static byte[] createJumpBootNoFlash(int id) {
		byte[] idArr = Utility.ByteArrayUtils.short2ByteArray((short) id);
		return Utility.ByteArrayUtils.concatAll(idArr, JUMP_BOOT_NO_FLASH);
	}

	public static byte[] createJumpUser(int id) {
		byte[] idArr = Utility.ByteArrayUtils.short2ByteArray((short) id);
		return Utility.ByteArrayUtils.concatAll(idArr, JUMP_USER);
	}

	public static byte[] createEraseFlash(int id, int pageCnt) {
		byte[] idArr = Utility.ByteArrayUtils.short2ByteArray((short) id);
		byte[] page = Utility.ByteArrayUtils.short2ByteArray((short) pageCnt);
		return Utility.ByteArrayUtils.concatAll(idArr, ERASE_FLASH, page);
	}

	public static byte[] createQueryEraseFlash(int id) {
		byte[] idArr = Utility.ByteArrayUtils.short2ByteArray((short) id);
		return Utility.ByteArrayUtils.concatAll(idArr, QUERY_ERASE);
	}

	public static byte[] createFlashPageStart() {
		byte[] idArr = Utility.ByteArrayUtils
				.short2ByteArray((short) BROADCAST_ID);
		return Utility.ByteArrayUtils.concatAll(idArr, PAGE_FLASH_START);
	}

	public static byte[] createFunctionFrame(int startAddr, int len, int pageNo) {
		byte[] idArr = Utility.ByteArrayUtils
				.short2ByteArray((short) BROADCAST_ID);
		byte[] addr = Utility.ByteArrayUtils.int2ByteArray(startAddr);
		byte[] length = Utility.ByteArrayUtils.short2ByteArray((short) len);
		byte[] pageNum = Utility.ByteArrayUtils.short2ByteArray((short) pageNo);
		return Utility.ByteArrayUtils.concatAll(idArr, length, addr, pageNum);
	}

	public static byte[] createDataFrame(byte[] data) {
		byte[] idArr = Utility.ByteArrayUtils
				.short2ByteArray((short) BROADCAST_ID);
		return Utility.ByteArrayUtils.concatAll(idArr, data);
	}

	public static byte[] createFlashFinish(int len) {
		byte[] idArr = Utility.ByteArrayUtils
				.short2ByteArray((short) BROADCAST_ID);
		byte[] length = Utility.ByteArrayUtils.short2ByteArray((short) len);
		return Utility.ByteArrayUtils.concatAll(idArr, PAGE_FLASH_FINISH, length);
	}

	public static byte[] createQueryPageFlash(int id) {
		byte[] idArr = Utility.ByteArrayUtils.short2ByteArray((short) id);
		return Utility.ByteArrayUtils.concatAll(idArr, QUERY_PAGE_FLASH);
	}
}
